package com.cfpl.admin_service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.admin_service.VO.PageQueryCondition;
import com.cfpl.admin_service.service.VegetableService;
import com.cfpl.project_common.pojo.Vegetable;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: wrp
 * @Date: 2023/10/18 - 10:11
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/vegetable")
@Api(tags = "果蔬相关接口")
public class VegetableController {

    @Autowired
    private VegetableService vegetableService;

    /**
     * 功能描述：分页查询果蔬，可以按照条件模糊查询
     * @param currentPage 当前页
     * @param pageSize 每页条数
     * @param condition 查询条件
     * @return  分页对象
     */
    @ApiOperation("分页查询所有果蔬")
    @GetMapping("/queryAllVeg")
    public BaseResult<PageQueryCondition> queryAll(int currentPage,int pageSize,String condition) {
        Page<Vegetable> page = vegetableService.queryPage(currentPage,pageSize,condition);
        PageQueryCondition queryCondition = new PageQueryCondition(page,condition);
        return BaseResult.ok(queryCondition);
    }

    /**
     * 功能描述：新增一条果蔬记录
     * @param vegetable 果蔬对象
     * @return
     */
    @ApiOperation("新增果蔬")
    @PostMapping("/addVegetable")
    public BaseResult<String> addVegetable(@RequestBody Vegetable vegetable) {
        vegetableService.addVegetable(vegetable);
        return BaseResult.ok("success");
    }

    /**
     * 功能描述：根据id查询具体的果蔬信息
     * @param id 果蔬id
     * @return
     */
    @ApiOperation("查询一条果蔬")
    @GetMapping("/queryVegetableById/{id}")
    public BaseResult<Vegetable> queryVegetableById(@PathVariable("id") Long id) {
        Vegetable vegetable = vegetableService.queryVegetableById(id);
        return BaseResult.ok(vegetable);
    }

    /**
     * 功能描述：根据id修改果蔬对象
     *
     * @param vegetable 要修改的果蔬对象
     * @return 修改后的果蔬对象
     */
    @ApiOperation("修改果蔬对象")
    @PutMapping("/modifyVegetableById")
    public BaseResult modifyVegetableById(@RequestBody Vegetable vegetable) {
        vegetableService.updateVegetable(vegetable);
        return BaseResult.ok(vegetable);
    }

    /**
     * 功能描述：根据id删除果蔬
     * @param id 果蔬id
     * @return
     */
    @ApiOperation("删除一条果蔬对象")
    @DeleteMapping("/deleteVegetableById/{id}")
    public BaseResult<String> deleteVegetableById(@PathVariable("id")Long id) {
        Vegetable vegetable = vegetableService.queryVegetableById(id);
        if (vegetable != null) {
            vegetableService.removeVegetableById(id);
        }
        return BaseResult.ok();
    }

    /**
     * 功能描述：批量删除果蔬
     * @param Ids 多个果蔬id
     * @return
     */
    @ApiOperation("批量删除果蔬")
    @DeleteMapping("/deleteVegetableList")
    public BaseResult<Vegetable> deleteVegetableList(@RequestParam("Ids")List<Long> Ids) {
        vegetableService.deleteBatchByIds(Ids);
        return BaseResult.ok();
    }
}
